package com.ybf.user.dal.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ybf.user.dal.entity.DataPermissionDO;
import com.ybf.user.dal.enums.DeleteStatusEnum;
import com.ybf.user.dal.mapper.DataPermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @Date
 * @Author
 **/
@Component
public class DataPermissionManager extends ServiceImpl<DataPermissionMapper, DataPermissionDO> {

    @Autowired
    private DataPermissionMapper dataPermissionMapper;

    public List<DataPermissionDO> selectInRoleIds(List<Long> roleIds) {
        return dataPermissionMapper.selectList(new LambdaQueryWrapper<DataPermissionDO>().in(DataPermissionDO::getRoleId, roleIds));
    }

    /**
     * 删除
     *
     * @author 向南
     * @date 14:08 2023/7/31
     * @param: roleId
     **/
    public void deleteByRoleId(Long roleId) {
        if (Objects.isNull(roleId)) {
            return;
        }
        dataPermissionMapper.update(null, new LambdaUpdateWrapper<DataPermissionDO>()
                .eq(DataPermissionDO::getRoleId, roleId)
                .set(DataPermissionDO::getDeleted, DeleteStatusEnum.DELETE.getValue()));
    }

    public List<DataPermissionDO> selectByRoleId(Long roleId) {
        if (Objects.isNull(roleId)) {
            return Collections.emptyList();
        }
        return dataPermissionMapper.selectList(new LambdaQueryWrapper<DataPermissionDO>().eq(DataPermissionDO::getRoleId, roleId));
    }
}
